<template>
    <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline" @keyup.enter.native="search()">
            <el-form-item label="赠品名称">
              <br> <br>
                <el-input style="margin-left:-70px;width:200px" v-model="formInline.giftName" placeholder></el-input>
            </el-form-item>
            <el-form-item label="赠品编码">
              <br> <br>
                <el-input style="margin-left:-70px;width:200px" v-model="formInline.giftCode" placeholder></el-input>
            </el-form-item>
            <el-form-item label="赠品类型">
              <br> <br>
                <el-select style="margin-left:-70px;width:200px" v-model="formInline.giftType">
                    <el-option label="全部" value></el-option>
                    <el-option label="实物赠品" value="1"></el-option>
                    <el-option label="虚拟赠品" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="赠品属性">
              <br> <br>
                <el-select style="margin-left:-70px;width:200px" v-model="formInline.giftProperty">
                    <el-option label="全部" value></el-option>
                    <el-option label="普通虚拟赠品" value="1"></el-option>
                    <el-option label="特殊虚拟赠品" value="2"></el-option>
                </el-select>
            </el-form-item>
            <div style="width:100%;display:flex;justify-content:flex-end;">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="search()" size="mini">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="toAdd()" icon="el-icon-edit" size="mini">新增</el-button>
            </el-form-item>
            </div>
            
        </el-form>

        <el-table
            :data="tableData"
            v-loading="loading"
            border
              height="400px"
            style="width: 100%;"
            :header-cell-style="{background:'#ebecf0',color:'#606266'}"
        >
            <el-table-column type="index" width="60" label="序号" align="center" ></el-table-column>
            <el-table-column prop="giftName" label="赠品名称" align="center" width="180"></el-table-column>
            <el-table-column prop="giftCode" label="赠品编码"></el-table-column>

            <el-table-column label="赠品类型" align="center" width="120">
                <template slot-scope="scope">
                    <span v-if="scope.row.giftType == '1'">实物赠品</span>
                    <span v-if="scope.row.giftType == '2'">虚拟赠品</span>
                </template>
            </el-table-column>
            <el-table-column label="赠品属性" align="center" width="120">
                <template slot-scope="scope">
                    <span v-if="scope.row.giftProperty == '1'">普通虚拟赠品</span>
                    <span v-if="scope.row.giftProperty == '2'">特殊虚拟赠品</span>
                </template>
            </el-table-column>

            <el-table-column prop="giftPic" label="赠品图片" align="center" width="180">
                <template slot-scope="scope">
                    <img
                        :src="scope.row.giftPic"
                        v-if="scope.row.giftPic != '' && scope.row.giftPic != null"
                        width="70"
                        height="50"
                    />
                </template>
            </el-table-column>
            <el-table-column prop="giftDesc" label="赠品描述" width="280"></el-table-column>
            <el-table-column label="状态" align="center" width="120">
                <template slot-scope="scope">
                    <span v-if="scope.row.status == '1'">启用</span>
                    <span v-if="scope.row.status == '0'">禁用</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                width="180"
                align="center"
                header-align="center"
                fixed="right"
            >
                <template slot-scope="scope">
                    <el-button type="text" @click="toEdit(scope.row, true,true)">查看</el-button>
                    <el-button
                        @click="handleClick(scope.row)"
                        type="text"
                        size="mini"
                        v-if="scope.row.status===0"
                    >启用</el-button>
                    <el-button
                        @click="handleClick(scope.row)"
                        type="text"
                        size="mini"
                        v-if="scope.row.status===1"
                    >禁用</el-button>

                    <el-button
                        type="text"
                        @click="toEdit(scope.row, false,true)"
                        v-if="scope.row.status===0"
                    >编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="block" style="margin-top:5px;">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[10, 20, 30, 50, 100, 150, 200]"
                :current-page="listQuery.current"
                :page-size="listQuery.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </div>
    </div>
</template>

<script>
import {
  queryPromotionList,
  updatePlanStatus,
  deletePromotion
} from "@/api/promotionProgramEdit";
import {
qryGiftBank,
editGiftBankStatus
} from "@/api/giftbank";

export default {
  data() {
    return {
      logininfo: {},
      orgList: [],
      orgSeq: "",
      loading: true,
      formInline: {
        giftName: "",
        giftCode: "",
           giftType: "",
        giftProperty: "",
      },
      total: 0,
      userId: "",
      listQuery: {
        current: 1,
        size: 100
      },
      tableData: []
    };
  },
  mounted() {
    this.logininfo = localStorage.getItem("logininfo");
    this.orgList = JSON.parse(this.logininfo).orgList;
    this.orgSeq = this.orgList[0].orgSeq;
    this.qryGiftBank();
  },
  methods: {
        handleClick(v) {
            console.log(v,"oi")
                  var msg = "";
      if (v.status === 0) {
        var status = 1;
           msg = "启用";
      } else if (v.status === 1) {
        var status = 0;
           msg = "禁用";
      }

        var stopMsg = "";
      if (status == "0"&&v.activityName!==null&&v.activityName!=="") {
        // 判断是否已被用于活动，是则给出提示
        var hd = v.activityName;
        // if (v.activityId !== null && v.activityId!=="") {
        //   hd = "赠品活动";
        // }
      
        stopMsg = ("该赠品已被用在" + hd +"活动中"+ "，"+"您");
      }
    //   this.$confirm(stopMsg + "确定" + msg + "当前优惠券吗？", "提示", {
    //     confirmButtonText: msg,
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
 
      let data = { uuid: v.uuid, status: status };
          this.$confirm(stopMsg+"确定" + msg + "当前赠品吗？").then(() => {
      editGiftBankStatus(data).then(res => {
          if(res.code==1){
  this.$message({
          showClose: true,
          message: "操作成功",
          type: "success"
        });
        this.qryGiftBank();
          }else{
              this.$message({
          showClose: true,
          message: res.message,
          type: "error"
        }); 
          }
      })
      });
    },
    kong() {
    
        this.formInline.giftName= "";
          this.formInline.giftCode= "";
       this.formInline.giftType= "";
   this.formInline.giftProperty= "";
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.qryGiftBank();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.qryGiftBank();
    },
    indexMethod(index) {
      let curpage = this.listQuery.current; //单前页码，具体看组件取值
      let limitpage = this.listQuery.size; //每页条数，具体是组件取值
      return index + 1 + (curpage - 1) * limitpage;
    },
    search() {
      this.listQuery.current = 1;
      this.qryGiftBank();
    },
    qryGiftBank() {
      let data = {
        size: this.listQuery.size,
        current: this.listQuery.current,
        condition: {
       
          giftName: this.formInline.giftName,
          giftCode: this.formInline.giftCode,
             giftType: this.formInline.giftType,
          giftProperty: this.formInline.giftProperty,
          
          activityType:0,
        },
        ascs: [],
        descs: []
      };
      qryGiftBank(data).then(res => {
        this.loading = false;
        this.tableData = res.records;
        this.total = res.total;
      });
    },
    getCurrentDate(){
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      var cur = year + (month < 10 ? '-0' : '-') + month + (date < 10 ? '-0' : '-') + date;
      return cur;
    },
    toAdd() {
      this.$router.push({
        name: "giftbankedit",
        params: {}
      });
    },
    toEdit(item, flag,flagone) {
      this.$router.push({
        name: "giftbankedit",
        params: {
          edit: item,
          disabledFlag: flag,
          disabledFlagone:flagone
        }
      });
    },
    editGiftBankStatus(uuid, status) {
    //   var flag = 0;
    //   for (var i in this.tableData) {
    //     // 当前时间段内有启用的活动，则不可启用
    //     var stts = this.tableData[i].status;
    //     var beg = this.tableData[i].beginTime;
    //     beg = beg.substr(0,10);
    //     var end = this.tableData[i].endTime;
    //     end = end.substr(0,10);
    //     var cur = this.getCurrentDate();
    //     console.log(beg + '至' + end + "|" + cur + "|" + stts);
    //     if (stts == 1 && beg <= cur && end >= cur) {
    //       flag = 1;
    //       break;
    //     }
    //   }
    //   if (status == "1" && flag == 1) {
    //     this.$alert('当前已有状态为发布的活动，要发布新的活动，请先撤回其他活动', '提示', {
    //       confirmButtonText: '知道了',
    //       callback: action => {}
    //     });
    //     return;
    //   }
      var msg = "";
      if (status == "1") {
        msg = "启用";
      } else if (status == "0") {
        msg = "禁用";
      } 
       var stopMsg = "";
      if (status == "0") {
        // 判断是否已被用于活动，是则给出提示
        var hd = "";
        if (item.promotionPlanDTOS != null && item.promotionPlanDTOS.length > 0) {
          hd = "赠品活动";
        }
      
        stopMsg = ("该券已被用于" + hd + "，");
      }
    //   this.$confirm(stopMsg + "确定" + msg + "当前优惠券吗？", "提示", {
    //     confirmButtonText: msg,
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   })
      this.$confirm(stopMsg+"确定" + msg + "当前赠品吗？", "提示", {
        confirmButtonText: msg,
        cancelButtonText: "取消",
        // type: "warning"
      })
        .then(() => {

          editGiftBankStatus(data).then(res => {
      
            if (res.code == "1") {
              this.$message({
                showClose: true,
                message: msg + "成功",
                type: "success"
              });
            //   this.promotionList();
            } else {
              this.$message.error(res.message);
            }
            this.loading = false;
          });
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="less" scoped>
.toptitle {
    border: 1px solid #e0e0e0;
    border-bottom: 2px solid #188097;
    background: #f5f5f5;
    line-height: 35px;
    margin-bottom: 10px;
    padding: 0 10px;
    font-size: 14px;
    font-weight: bold;
}
.el-input--small .el-input__inner {
    height: 32px;
    line-height: 32px;
}
.demo-form-inline {
    //   border: 1px solid #ebeef5;
    padding: 20px 10px 0px 30px;
}
</style>